<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Tweet Model
 * @description : Database functions related to tweets, replies
 * 
 */
class Tweet_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  function get_tweets($limit = 5, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('tweets.*, users.*', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweets.tweet_id', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_tweets_count($limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('count(*) as total_tweets_count', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweets.tweet_id', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row()->total_tweets_count;
      $result_data['count'] = $query_exec->row()->total_tweets_count;
    }
    return $result_data;
  }

  /**
   * Search tweets
   * 
   */
  function search_tweets($search = NULL, $limit = 5, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $search_user_id = NULL;
    $search_users = get_mentioned_users($search);
    if (!empty($search_users) && !empty($search_users[0]['user_id'])) {
      $search_user_id = $search_users[0]['user_id'];
    }
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('tweets.*, users.*', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweets.tweet_id', 'desc');
    if (!empty($search_user_id)) {
      $this->db->where('tweet_user_id', $search_user_id);
    } else {
      $this->db->like('tweet_subject', $search);
      $this->db->or_like('tweet_message', $search);
    }
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function search_tweets_count($search = NULL, $limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $search_user_id = NULL;
    $search_users = get_mentioned_users($search);
    if (!empty($search_users) && !empty($search_users[0]['user_id'])) {
      $search_user_id = $search_users[0]['user_id'];
    }
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('count(*) as total_tweets_count', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweets.tweet_id', 'desc');
    if (!empty($search_user_id)) {
      $this->db->where('tweet_user_id', $search_user_id);
    } else {
      $this->db->like('tweet_subject', $search);
      $this->db->or_like('tweet_message', $search);
    }
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row()->total_tweets_count;
      $result_data['count'] = $query_exec->row()->total_tweets_count;
    }
    return $result_data;
  }

  function get_top_tweets($limit = 5, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('tweets.*, users.*', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweet_replies_count', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_top_tweets_count($limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('count(*) as total_tweets_count', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->order_by('tweet_replies_count', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row()->total_tweets_count;
      $result_data['count'] = $query_exec->row()->total_tweets_count;
    }
    return $result_data;
  }

  function get_active_tweets($limit = 5, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('tweets.*, users.*', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->select('(SELECT MAX(tweet_reply_date) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS latest_tweet_reply', FALSE);
    $this->db->order_by('latest_tweet_reply', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_active_tweets_count($limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['tweets.tweet_status'] = 1;
    $query_cond['users.user_status'] = 1;
    $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
    $this->db->select('count(*) as total_tweets_count', FALSE);
    $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
    $this->db->select('(SELECT MAX(tweet_reply_date) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS latest_tweet_reply', FALSE);
    $this->db->order_by('latest_tweet_reply', 'desc');
    $query_exec = $this->db->get_where('tweets', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row()->total_tweets_count;
      $result_data['count'] = $query_exec->row()->total_tweets_count;
    }
    return $result_data;
  }

  function get_my_tweets($user_id = NULL, $limit = 5, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['tweets.tweet_status'] = 1;
      $query_cond['tweets.tweet_user_id'] = $user_id;
      $query_cond['users.user_status'] = 1;
      $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
      $this->db->select('tweets.*, users.*', FALSE);
      $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
      $this->db->order_by('tweets.tweet_id', 'desc');
      $query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_my_tweets_count($user_id = NULL, $limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['tweets.tweet_status'] = 1;
      $query_cond['tweets.tweet_user_id'] = $user_id;
      $query_cond['users.user_status'] = 1;
      $this->db->join('users', 'users.user_id = tweets.tweet_user_id');
      $this->db->select('count(*) as total_tweets_count', FALSE);
      $this->db->select('(SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_replies_count', FALSE);
      $this->db->order_by('tweets.tweet_id', 'desc');
      $query_exec = $this->db->get_where('tweets', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row()->total_tweets_count;
        $result_data['count'] = $query_exec->row()->total_tweets_count;
      }
    }
    return $result_data;
  }

  function get_my_replies($user_id = NULL, $limit = 5) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['tweet_reply_status'] = 1;
      $query_cond['tweet_reply_user_id'] = $user_id;
      $query_cond['tweet_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('tweets', 'tweet_id = tweet_reply_tweet_id');
      $this->db->join('users', 'user_id = tweet_reply_user_id');
      $this->db->order_by('tweets_reply.tweet_reply_id', 'desc');
      $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_by_id($tweet_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweets.tweet_id'] = $tweet_id;
      $query_cond['tweets.tweet_status'] = 1;
      $query_cond['users.user_status'] = 1;
      $this->db->join('users', 'users.user_id = tweets.tweet_user_id ');
      $query_exec = $this->db->get_where('tweets', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_reply_by_id($tweet_id = NULL, $limit = NULL, $sort_by = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweets_reply.tweet_reply_tweet_id'] = $tweet_id;
      $query_cond['tweets_reply.tweet_reply_status'] = 1;
      $query_cond['users.user_status'] = 1;
      $this->db->select('tweets_reply.*, users.*', FALSE);
      $this->db->select('(SELECT COUNT(like_id) FROM likes WHERE like_type_id = tweets_reply.tweet_reply_id AND like_type = "reply") AS reply_likes_count', FALSE);
      $this->db->join('users', 'users.user_id = tweets_reply.tweet_reply_user_id');
      if ($sort_by == 2) {
        $this->db->order_by('reply_likes_count', 'desc');
      } else {
        $this->db->order_by('tweets_reply.tweet_reply_id', 'desc');
      }
      $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_deleted_replies($tweet_id = NULL, $limit = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweet_reply_tweet_id'] = $tweet_id;
      $query_cond['tweet_reply_status'] = 0;
      $this->db->join('users', 'users.user_id = tweets_reply.tweet_reply_user_id');
      $this->db->order_by('tweets_reply.tweet_reply_id', 'desc');
      $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_reply_by_id($reply_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($reply_id) && is_numeric($reply_id)) {
      $query_cond = array();
      $query_cond['tweets_reply.tweet_reply_id'] = $reply_id;
      $query_cond['tweets_reply.tweet_reply_status'] = 1;
      $this->db->join('users', 'user_id = tweet_reply_user_id');
      $query_exec = $this->db->get_where('tweets_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function set_tweet($tweet_subject = NULL, $tweet_msg = NULL, $tweet_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_subject) && !empty($tweet_msg) && !empty($tweet_user_id) && is_numeric($tweet_user_id)) {
      $query_insr = array(
          'tweet_subject' => $tweet_subject,
          'tweet_message' => $tweet_msg,
          'tweet_user_id' => $tweet_user_id,
          'tweet_date' => date("Y-m-d H:i:s")
      );
      $query_exec = $this->db->insert('tweets', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['tweet_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function set_tweet_reply_by_id($tweet_id = NULL, $tweet_reply_msg = NULL, $tweet_reply_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($tweet_reply_msg) && !empty($tweet_reply_user_id) && is_numeric($tweet_reply_user_id)) {
      $query_insr = array(
          'tweet_reply_tweet_id' => $tweet_id,
          'tweet_reply_message' => $tweet_reply_msg,
          'tweet_reply_user_id' => $tweet_reply_user_id,
          'tweet_reply_date' => date("Y-m-d H:i:s")
      );
      $query_exec = $this->db->insert('tweets_reply', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function del_tweet_by_id($tweet_id = NULL, $tweet_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($tweet_user_id) && is_numeric($tweet_user_id)) {
      $query_cond = array();
      $query_cond['tweet_id '] = $tweet_id;
      $query_cond['tweet_user_id '] = $tweet_user_id;
      $query_upda = array();
      $query_upda['tweet_status'] = '0';
      $query_exec = $this->db->update('tweets', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function delete_tweet($tweet_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweet_id '] = $tweet_id;
      $query_upda = array();
      $query_upda['tweet_status'] = '0';
      $query_exec = $this->db->update('tweets', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function update_tweet_by_id($tweet_id = NULL, $tweet_subject = NULL, $tweet_msg = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($tweet_subject) && !empty($tweet_msg)) {
      $query_upda = array();
      $query_upda['tweet_subject'] = $tweet_subject;
      $query_upda['tweet_message'] = $tweet_msg;
      $query_cond = array();
      $query_cond['tweet_id '] = $tweet_id;
      $query_exec = $this->db->update('tweets', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function del_tweet_reply_by_id($tweet_reply_id = NULL, $tweet_reply_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_reply_id) && is_numeric($tweet_reply_id) && !empty($tweet_reply_user_id) && is_numeric($tweet_reply_user_id)) {
      $query_cond = array();
      $query_cond['tweet_reply_id'] = $tweet_reply_id;
      $query_cond['tweet_reply_user_id'] = $tweet_reply_user_id;
      $query_upda = array();
      $query_upda['tweet_reply_status'] = '0';
      $query_exec = $this->db->update('tweets_reply', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function delete_reply($tweet_reply_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_reply_id) && is_numeric($tweet_reply_id)) {
      $query_cond = array();
      $query_cond['tweet_reply_id'] = $tweet_reply_id;
      $query_upda = array();
      $query_upda['tweet_reply_status'] = '0';
      $query_exec = $this->db->update('tweets_reply', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function upd_tweet_notify_status($tweet_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweet_id'] = $tweet_id;
      $query_upda = array();
      $query_upda['tweet_notification'] = $notify_status;
      $query_exec = $this->db->update('tweets', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function upd_tweet_reply_notify_status($tweet_reply_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_reply_id) && is_numeric($tweet_reply_id)) {
      $query_cond = array();
      $query_cond['tweet_reply_id'] = $tweet_reply_id;
      $query_upda = array();
      $query_upda['tweet_reply_notification'] = $notify_status;
      $query_exec = $this->db->update('tweets_reply', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_all_tweet_users() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query = "
        SELECT users.*,
        (SELECT COUNT(tweet_id) FROM tweets WHERE tweet_user_id = users.user_id AND tweet_status = 1) AS user_tweets_count,
        (SELECT COUNT(tweet_reply_id) FROM tweets_reply tr JOIN tweets t ON t.tweet_id = tr.tweet_reply_tweet_id WHERE tweet_reply_user_id = users.user_id AND t.tweet_status = 1 AND tr.tweet_reply_status = 1) AS user_replies_count
        FROM users
        ORDER BY (user_tweets_count + user_replies_count) DESC
        LIMIT 10
        ";
    $query_exec = $this->db->query($query);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_all_tweet_extras($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $date_dmy = date('Y-m-d');
    if (!empty($user_id) && is_numeric($user_id)) {
      $query = "
        SELECT
        (SELECT COUNT(tweet_id) FROM tweets WHERE tweet_status = 1 LIMIT 1) AS tweet_total_count,
        (SELECT COUNT(tweet_reply_id) FROM tweets_reply tr JOIN tweets t ON t.tweet_id = tr.tweet_reply_tweet_id WHERE tweet_status = 1 AND tweet_reply_status = 1 LIMIT 1) AS tweet_total_replies_count,
        (SELECT COUNT(tweet_id) FROM tweets WHERE tweet_user_id = $user_id AND tweet_status = 1 LIMIT 1) AS my_tweet_total_count,
        (SELECT COUNT(tweet_reply_user_id) FROM tweets_reply tr JOIN tweets t ON t.tweet_id = tr.tweet_reply_tweet_id  WHERE tweet_reply_user_id = $user_id AND tweet_status = 1 AND tweet_reply_status = 1 LIMIT 1) AS tweet_my_replies_count,
        (SELECT COUNT(tweet_id) FROM tweets t1 WHERE (t1.tweet_status = 1) AND (DATE(t1.tweet_date) = '{$date_dmy}') AND ((SELECT COUNT(tr1.tweet_reply_id) FROM tweets_reply tr1 WHERE t1.tweet_id = tr1.tweet_reply_tweet_id AND tr1.tweet_reply_status = 1) = 0) LIMIT 1) AS new_tweets_count
        FROM tweets LIMIT 1
        ";
      $query_exec = $this->db->query($query);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_users($tweet_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query = "
        SELECT users.*,
        (SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = $tweet_id AND tweet_reply_user_id = users.user_id AND tweet_reply_status = 1) AS user_replies_count
        FROM tweets_reply
        RIGHT JOIN users ON users.user_id = tweets_reply.tweet_reply_user_id
        WHERE tweets_reply.tweet_reply_tweet_id = $tweet_id AND tweets_reply.tweet_reply_status = 1
        GROUP BY tweets_reply.tweet_reply_user_id
        ORDER BY user_replies_count DESC
        ";
      $query_exec = $this->db->query($query);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_extras($tweet_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id) && !empty($user_id) && is_numeric($user_id)) {
      $query = "
        SELECT tweets.*,
        (SELECT COUNT(tweet_reply_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_status = 1) AS tweet_total_replies_count,
        (SELECT COUNT(tweet_reply_user_id) FROM tweets_reply WHERE tweet_reply_tweet_id = tweets.tweet_id AND tweet_reply_user_id = $user_id AND tweet_reply_status = 1) AS tweet_my_replies_count,
        (SELECT user_display_name FROM users WHERE user_id = tweets.tweet_user_id AND tweet_status = 1) AS tweet_owner_dispaly_name
        FROM tweets
        WHERE tweets.tweet_id = $tweet_id
        ";
      $query_exec = $this->db->query($query);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_new_replies($tweet_id = NULL, $last_reply_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweets_reply.tweet_reply_tweet_id'] = $tweet_id;
      $query_cond['tweets_reply.tweet_reply_status'] = 1;
      $query_cond['users.user_status'] = 1;
      $this->db->join('users', 'users.user_id = tweets_reply.tweet_reply_user_id');
      $this->db->order_by('tweets_reply.tweet_reply_id', 'asc');
      $this->db->where('tweets_reply.tweet_reply_id >', $last_reply_id);
      $query_exec = $this->db->get_where('tweets_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_tweet_old_replies($tweet_id = NULL, $last_reply_id = NULL, $sort_by = 1, $limit = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['tweets_reply.tweet_reply_tweet_id'] = $tweet_id;
      $query_cond['tweets_reply.tweet_reply_status'] = 1;
      $query_cond['users.user_status'] = 1;
      $this->db->select('tweets_reply.*, users.*', FALSE);
      $this->db->select('(SELECT COUNT(like_id) FROM likes WHERE like_type_id = tweets_reply.tweet_reply_id AND like_type = "reply") AS reply_likes_count', FALSE);
      $this->db->join('users', 'users.user_id = tweets_reply.tweet_reply_user_id');
      if ($sort_by == 2) {
        $this->db->order_by('reply_likes_count', 'desc');
        if (!empty($limit)) {
          $limit = $last_reply_id;
        }
        $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit, 10);
      } else {
        $this->db->order_by('tweets_reply.tweet_reply_id', 'desc');
        $this->db->where('tweets_reply.tweet_reply_id <', $last_reply_id);
        $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit);
      }
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function add_follow($tweet_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && !empty($user_id) && is_numeric($tweet_id) && is_numeric($user_id)) {
      $query_insr = array(
          'follow_tweet_id' => $tweet_id,
          'follow_user_id' => $user_id,
          'follow_date' => date("Y-m-d H:i:s")
      );
      $query_sql = $this->db->insert_string('follows', $query_insr);
      $query_sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $query_sql);
      $query_exec = $this->db->query($query_sql);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function del_follow($tweet_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && !empty($user_id) && is_numeric($tweet_id) && is_numeric($user_id)) {
      $query_cond = array(
          'follow_tweet_id' => $tweet_id,
          'follow_user_id' => $user_id
      );
      $query_exec = $this->db->delete('follows', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_follow($tweet_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && !empty($user_id) && is_numeric($tweet_id) && is_numeric($user_id)) {
      $query_cond = array(
          'follow_tweet_id' => $tweet_id,
          'follow_user_id' => $user_id
      );
      $this->db->select('follows.*', FALSE);
      $this->db->select('(SELECT COUNT(follow_id) FROM follows WHERE follow_tweet_id = ' . $tweet_id . ') AS tweet_follow_count', FALSE);
      $query_exec = $this->db->get_where('follows', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_followers($tweet_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id)) {
      $query_cond = array();
      $query_cond['follow_tweet_id'] = $tweet_id;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = follow_user_id');
      $query_exec = $this->db->get_where('follows', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Load more
  function load_more_replies($tweet_id = NULL, $last_loaded_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($tweet_id) && is_numeric($tweet_id) && is_numeric($last_loaded_id) && is_numeric($limit)) {
      $query_cond = array();
      $query_cond['tweets_reply.tweet_reply_tweet_id'] = $tweet_id;
      $query_cond['tweets_reply.tweet_reply_status'] = 1;
      $query_cond['users.user_status'] = 1;
      $this->db->join('users', 'users.user_id = tweets_reply.tweet_reply_user_id');
      $this->db->order_by('tweets_reply.tweet_reply_id', 'asc');
      $this->db->where('tweets_reply.tweet_reply_id <', $last_loaded_id);
      $query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

}

?>
